__author__ = 'DI'

import json
import codecs

class Files():
    "file manipulations"
    def __init__(self, filename):
        self.file=codecs.open(filename,'rU','utf-8')

    def get_line(self):
        return self.file.readline()

class tweetContainer():
    "Contains twitter posts loaded from a file"
    def __init__(self, file):
        self.posts=[]
        self.file=file

    def load_posts(self):
        "Load all English twitter post"
        while True:
            line=self.file.get_line()
            if not line : break
            self.posts.append(json.loads(line))

    def en_post(self,post):
        "Return true if the twitter post is in english"
        if post['lang'] == "en":
            return True
        else:
            return False

    def get_id(self):
        "Returns the id of the fist tweet"
        return self.posts[0]['id']

    def get_short_post(self):
        "Returns the following for the first post in the container screen name, tine of posting, text and geo"
        return str(self.posts[0]['user']['screen_name']).strip('[] "\'')+\
               (str(self.posts[0]['created_at']).strip('[] "\''))+"\n"+\
               (str(self.posts[0]['text']).strip('[] "\''))+"\n"+\
               (str(self.posts[0]['geo']).strip('[] "\'')+"\n")

    def del_post(self):
        "Removes first post in the container"
        del self.posts[0]


    def have_posts(self):
        ""
        if len(self.posts):
            return True
        else:
            return False

input=Files("stream.txt")
tweets=tweetContainer(input)
tweets.load_posts()
while tweets.have_posts():
    print(tweets.get_short_post())
    codecs.open("posts.txt",'a+','utf-8').write(tweets.get_short_post())
    tweets.del_post()

print("Container Size:", len(tweets.posts))
